# Kestrel: An XMPP-based Many-Task Computing Scheduler
# Author: Lance Stout <lancestout@gmail.com>
#
# Copyright 2009-2010 Clemson University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Analyzer Role

The analyzer role is responsible for reading logs from past
application runs and generating plots or other useful information.
"""

from kestrel.analyzer import LogReader, Plotter


class AnalyzerRole(object):
    """
    The analyzer role is responsible for reading logs from past
    application runs and generating plots or other useful information.

    Configuration entries for the modules in this role:
      analyzer/log

    Events raised by this module:
      analyzer/start
      analyer/stop
    """
    def __init__(self):
        """
        Set the name of the role and define the default module
        implementations.
        """
        self.name = 'analyzer'
        self.defaults = {'analyzer/logreader': LogReader,
                         'analyzer/plotter': Plotter}
        self.modules = {}

    def register(self, kernel):
        """
        Instantiate and register the role's modules. The actual
        module implementations can be swapped by setting the
        appropriate configuration entries.
        """
        for module in self.defaults:
            impl = kernel.config(module, self.defaults[module])
            self.modules[module] = impl(kernel)

        kernel.event('analyzer/start')

    def unregister(self, kernel):
        """Unregister the role's modules from the kernel."""
        kernel.event('analyzer/shutdown')
